import GameDataModel from "./GameDataModel";

/*
如果一个元胞有两个或三个活邻居，它会保持或变为活跃状态。
如果一个元胞有少于两个活邻居，它会死亡。
如果一个元胞有超过三个活邻居，它也会死亡。
如果一个元胞是死的，且恰好有三个活邻居，它会变为活的。
*/
const {ccclass, property} = cc._decorator;

@ccclass
export default class CellManager extends cc.Component {

    @property(cc.Sprite)
    tempSprite: cc.Sprite = null;

    @property(cc.Sprite)
    cellSprite: cc.Sprite = null;

    @property(cc.Camera)
    camera: cc.Camera = null;
    
    private updateInter  = 0.03;               //更新间隔 ，你可以改这个值 1s 30zheng
    private countTime = 0;  

    private tempMaterial :cc.Material = null;

    protected start(): void {
        this.tempMaterial = this.tempSprite.getMaterial(0);

        GameDataModel.getInstance().createNewMap();
        GameDataModel.getInstance().randomMap();
        this.init();
    }

    //随机地图
    init(){
        let newMap = GameDataModel.getInstance().mapData;
        let MapHeight = GameDataModel.getInstance().mapHeight;
        let MapWidth = GameDataModel.getInstance().mapWidth;

   
        let data = new Uint8Array(MapHeight * MapWidth * 4);

        for (let i = 0; i < MapHeight; i++) {
            for (let j = 0; j < MapWidth; j++) {
                // 计算当前像素的起始索引，每个像素占 4 字节
                let index = (i * MapWidth + j) * 4; 
                
                // 判断元胞状态，填充 RGBA 通道
                let cellValue = newMap[i][j] === 1 ? 255 : 0;  // 如果是活元胞，r 通道为 255，否则为 0
                
                // 将 RGBA 通道分别赋值
                data[index] = cellValue;      // R 通道
                data[index + 1] = cellValue;          // G 通道
                data[index + 2] = cellValue;          // B 通道
                data[index + 3] = 255;        // A 通道（完全不透明）
            }
        }
        
        // 获取纹理
        let texture = new cc.Texture2D();
        texture.packable = false;
        
        texture.initWithData(data, cc.Texture2D.PixelFormat.RGBA8888, MapWidth, MapHeight);
        

        // 将纹理传递给材质
        this.tempMaterial.setProperty('cellTexture', texture, 0, true);

        this.updateCell(true);
    }

    updateCell( isFlipY ) {
        
        this.render();
        let texture = this.camera.targetTexture;

        texture.setFlipY(false);

        this.tempMaterial.setProperty('isFlipY', isFlipY, 0, true);
        this.tempMaterial.setProperty('cellTexture', texture, 0, true);
  
        let spriteFrame = new cc.SpriteFrame();
        spriteFrame.setTexture(texture);
        this.cellSprite.spriteFrame = spriteFrame;

        this.render();
    }

    private render(){
        let renderTexture = new cc.RenderTexture();

        // //获取摄像机的内容
        renderTexture.initWithSize(GameDataModel.getInstance().mapWidth, GameDataModel.getInstance().mapHeight);
        this.camera.targetTexture = renderTexture;
        this.camera.render();
    }


    protected update(dt: number): void {
        this.countTime += dt;
        if(this.countTime >= this.updateInter){
            this.updateCell( true);
            this.countTime = 0;
        }
    }

}
